<?php defined('SYSPATH') or die('No direct script access.');

/**
 * 课时接口
 * 
 * @author 陈蕾
 */
class Controller_Lesson extends Controller_Base
{
	/**
	 * 课时模型对象
	 */
	private $_lesson;
	
	//==========================================================================
	// Hooks	
	//==========================================================================
	
	public function before()
	{
		parent::before();
		if (self::DEBUG) {
			$this->_params['client_id'] = 'ch01_01';
		}
	}
	
	//==========================================================================
	// Actions
	//==========================================================================
	
	/**
	 * 学习指定课时
	 */
	public function action_enter()
	{
		if (!$this->verify()) {
			return;
		}
		
		$now = date('Y-m-d H:i:s');
		
		// 新增学习日志
		$log = Model::factory('lessonlog');
		$log->member_id    = $this->id;
		$log->chapter_id   = $this->_lesson->chapter_id;
		$log->lesson_id    = $this->_lesson->id;
		$log->created      = $now;
		$log->last_actived = $now;
		$log->save();
		
		$progress = $this->getProgress();
		if (!$progress->loaded()) {
			// 新增学习进度记录
			$progress->member_id  = $this->id;
			$progress->chapter_id = $this->_lesson->chapter_id;
			$progress->lesson_id  = $this->_lesson->id;
			$progress->state      = Model_LessonProgress::STATE_ING;
		}
		
		// 更新学习进度
		$progress->last_study = $now;
		$progress->curr_log   = $log->id;
		$progress->save();
		
		$detail = $this->reduceResult(
			$this->_lesson->as_array(),
			array('name', 'content', 'image')
		);
		
		// 获取学习进度
		$progress->reload();
		$detail['progress'] = $this->reduceResult(
			$progress->as_array(),
			array('state', 'last_study', 'exam_score')
		);
		
		$detail['progress']['time_took'] = Model_LessonLog::getTook(
			$this->id, $this->_lesson->id
		);
		
		$this->output($detail);
	}
	
	/**
	 * 保存耗时
	 */
	public function action_dida()
	{
		if (!$this->verify()) {
			return;
		}
		
		$progress = $this->getProgress();
		if (!$progress->loaded()) {
			$this->error('进度不存在！');
			return;
		}
		
		if ($progress->curr_log) {
			$log = Model::factory('lessonlog');
			$log->find($progress->curr_log);
			if ($log->loaded()) {
				$log->last_actived = date('Y-m-d H:i:s');
				$log->calDuration();
				$log->save();
			}
		}
		
		$this->output(1);
	}
	
	/**
	 * 完成课时
	 */
	public function action_finish()
	{
		if (!$this->verify()) {
			return;
		}
		
		$progress = $this->getProgress();
		if (!$progress->loaded()) {
			$this->error('进度不存在！');
			return;
		}
		
		$progress->state = Model_LessonProgress::STATE_DONE;
		$progress->save();
		
		$this->output(1);
	}
	
	/**
	 * 保存测验分数
	 */
	public function action_exam_score()
	{
		// 测试数据
		$this->test(array(
			'score' => 75
		));
		
		if (!$this->verify()) {
			return;
		}
		
		$score = intval($this->get('score'));
		if ($score < 0) {
			$this->error('测验分数格式有误！');
			return;
		}
		
		$progress = $this->getProgress();
		if (!$progress->loaded()) {
			$this->error('进度不存在！');
			return;
		}
		
		// 写入日志
		$log = Model::factory('lessonlog');
		$log->find($progress->curr_log);
		if ($log->loaded()) {
			// 保存一次成绩
			if ($log->exam_score == -1) {
				$log->exam_score = $score;
				$log->save();
			}
		}
		
		// 之前未测验或者不几个时保存测验成绩
		if ($progress->exam_score < 60) {
			$progress->exam_score = $score;
			
			// 第一次及格的话本次的LogId
			if ($score >= 60) {
				$progress->exam_passed_id = $progress->curr_log;
			}
		}
		
		$progress->save();
		
		$this->output(1);
	}
	
	/**
	 * 获取学习轨迹
	 */
	public function action_track()
	{
		// 测试数据
		$this->test(array(
			'lesson_id' => 1
		));
		
		if (!$this->verify()) {
			return;
		}
		
		$progress = $this->getProgress();
		
		$logs = Model_LessonLog::getListByMember($this->id, $this->_lesson->id);
		
		$list = array();
		foreach ($logs as $log) {
			$passed = $log['id'] == $progress->exam_passed_id ? 1 : 0;
			
			$record = $this->reduceResult($log, array(
				'created', 'last_actived', 'duration', 'exam_score'
			));
			
			$record['first_passed'] = $passed;
			
			$list[] = $record;
		}
		
		$this->output($list);
	}
	
	//==========================================================================
	// Others
	//==========================================================================
	
	/**
	 * 检查必须的参数
	 * 包括：id, client_id
	 */
	private function verify()
	{
		if (!$this->id) {
			$this->error('未知用户！');
			return false;
		}
		
		$client_id = $this->get('client_id');
		if (!$client_id) {
			$this->error('请指定具体的课时！');
			return false;
		}
		
		$this->_lesson = ORM::factory('lesson');
		$this->_lesson->where('client_id', '=', $client_id)->find();
		if (!$this->_lesson->loaded()) {
			$this->error('课时不存在！');
			return false;
		}
		
		return true;
	}
	
	/**
	 * 获取进度模型的实例
	 */
	private function getProgress()
	{
		$progress = Model::factory('lessonprogress');
		$progress->where('member_id', '=', $this->id)
			->and_where('lesson_id', '=', $this->_lesson->id)->find();
		return $progress;
	}
}